
{{template "header" .}}

<div class="ibox float-e-margins" >
    <div class="row">
        <div class="col-lg-8"></div>
        <div class="col-lg-4"></div>

    </div>

    <div class="row">

        <div class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>
                        History List
                        {{if ne .TableName ""}}
                            {{.TableName}} -
                        {{end}}
                        {{if ne .SchemaName ""}}
                            {{.SchemaName}} -
                        {{end}}

                    </h5>

                    <!--DbName list-->
                    <div style="float: left; margin-top: -10px; margin-left: 10px">
                        <select class="form-control" name="DbName" id="DbName" {{if ne .SchemaName ""}}disabled{{end}}>
                            <option value="" > 数据源 </option>
                            {{range $i, $db := .DbList}}
                                <option value="{{$db.Name}}"> {{$db.Name}} </option>
                            {{end}}
                        </select>

                    </div>
                    <script type="text/javascript">$("#DbName").val("{{.DbName}}")</script>

                    <div style="float: left; margin-top: -10px; margin-left: 10px">
                        <select class="form-control" name="status" id="status">
                            {{range $i, $status := .StatusList}}
                                <option value="{{$status}}"> {{$status}} </option>
                            {{end}}
                        </select>

                    </div>
                    <script type="text/javascript">$("#status").val("{{.Status}}")</script>


                    <div class="ibox-tools">
                        <a class="collapse-link">
                            <i class="fa fa-chevron-up"></i>
                        </a>
                        <a class="close-link">
                            <i class="fa fa-times"></i>
                        </a>
                    </div>
                </div>
                <div class="ibox-content">
                    <div class="table-responsive">
                        <table class="table table-striped">
                            <thead>
                            <tr>
                                <th>ID</th>
                                <th>DbName</th>
                                <th>SchemaName</th>
                                <th>TableName</th>
                                <th>Other</th>
                                <th>Property</th>
                                <th>NowStartI</th>
                                <th>SelectThreadPool</th>
                                <th>ToServerIDList</th>
                                <th>SyncThreadCount</th>
                                <th>SyncToServer</th>
                                <th>StartTime</th>
                                <th>OverTime</th>
                                <th>Status</th>
                                <th>OP</th>
                            </tr>
                            </thead>
                            <tbody>
                            {{range $i, $v := .HistoryList}}
                                <tr id="ToServer_{{$i}}">
                                    <td>{{$v.ID}}</td>
                                    <td>{{$v.DbName}}</td>
                                    <td>{{$v.SchemaName}}</td>
                                    <td>
                                        <p>{{$v.TableName}}</p>
                                        {{if ne $v.TableName $v.TableNames}}
                                            {{range $k,$tableInfo := $v.TableNameArr}}
                                                {{if eq $k $v.TableCountSuccess }}
                                                    <p style="color: #F00; font-weight: bold" title="current table select ( RowsCount / SelectedCount )">{{$tableInfo.TableName}} ({{$tableInfo.RowsCount}} / {{$tableInfo.SelectCount}})</p>
                                                {{end}}
                                                {{if lt $k $v.TableCountSuccess }}
                                                    <p style="color: #1ab394" title="select over ( RowsCount / SelectedCount )" >{{$tableInfo.TableName}} ({{$tableInfo.RowsCount}} / {{$tableInfo.SelectCount}}) </p>
                                                {{end}}
                                                {{if gt $k $v.TableCountSuccess }}
                                                    <p>{{$tableInfo.TableName}}</p>
                                                {{end}}
                                            {{end}}
                                        {{end}}
                                    </td>
                                    <td>
                                        <p>CurrentTableName：{{$v.CurrentTableName}}</p>
                                        <p>TableCount：{{$v.TableCount}}</p>
                                        <p>SuccessCount：{{$v.TableCountSuccess}}</p>
                                        <p title="已经成功拉取条数">SelectRowsCount：{{$v.SelectRowsCount}}</p>
                                    </td>
                                    <td>
                                        <p>ThreadNum: {{$v.Property.ThreadNum}}</p>
                                        <p>ThreadCountPer: {{$v.Property.ThreadCountPer}}</p>
                                        <p>LimitOptimize: {{$v.Property.LimitOptimize}}</p>
                                        <p>Where:</p>
                                        <p>{{$v.Property.Where}}</p>
                                    </td>
                                    <td>{{$v.NowStartI}}</td>
                                    <td>
                                        {{range $k,$poolInfo := $v.ThreadPool}}
                                            <p>Num:{{$poolInfo.Num}}</p>
                                            <p>NowStartI:{{$poolInfo.NowStartI}}</p>
                                            <p>Error:{{$poolInfo.Error}}</p>
                                            <p>&nbsp;</p>
                                        {{end}}

                                    </td>
                                    <td>
                                        {{ $len := len $v.ToServerList }}
                                        {{if eq $len 0}}
                                            {{$v.ToServerIDList}}
                                        {{else}}
                                            {{range $k,$toServer := $v.ToServerList}}
                                                <p>ToServerID:{{$toServer.ToServerInfo.ToServerID}}</p>
                                                <p>PluginName:{{$toServer.ToServerInfo.PluginName}}</p>
                                                <p>ThreadCount:{{$toServer.ToServerInfo.ThreadCount}}</p>
                                                <p>Error:{{$toServer.ToServerInfo.Error}}</p>
                                            {{end}}
                                        {{end}}
                                    </td>
                                    <td>{{$v.ToServerTheadCount}}</td>
                                    <td>
                                        {{range $k,$toServer := $v.ToServerList}}
                                            <p>ToServerID:{{$toServer.ToServerInfo.ToServerID}}</p>
                                            <p>ToServerKey:{{$toServer.ToServerInfo.ToServerKey}}</p>
                                            <p>PluginName:{{$toServer.ToServerInfo.PluginName}}</p>
                                            <p>QueueMsgCount:{{$toServer.ToServerInfo.QueueMsgCount}}</p>
                                            <p>ThreadCount:{{$toServer.ToServerInfo.ThreadCount}}</p>
                                            <p>Error:{{$toServer.ToServerInfo.Error}}</p>
                                            <p>ErrorWaitData:{{$toServer.ToServerInfo.ErrorWaitData}}</p>
                                            <p>&nbsp;</p>
                                        {{end}}

                                    </td>
                                    <td>{{$v.StartTime}}</td>
                                    <td>{{$v.OverTime}}</td>
                                    <td>{{$v.Status}}</td>
                                    <td>
                                        <p>
                                            {{if eq $v.Status "running"}}
                                                <button data-toggle="button" class="btn-sm btn-warning stopBtn" type="button" onclick="DoChangeHistoryStatus(this,'stop')" >Stop</button>
                                                <button data-toggle="button" class="btn-sm btn-danger stopBtn" type="button" onclick="DoChangeHistoryStatus(this,'kill')" >Kill</button>
                                            {{else if eq $v.Status "stoping"}}
                                                <button data-toggle="button" class="btn-sm btn-danger stopBtn" type="button" onclick="DoChangeHistoryStatus(this,'kill')" >Kill</button>
                                            {{else if eq $v.Status "stoped"}}
                                                <button data-toggle="button" class="btn-sm btn-danger delBtn" type="button" onclick="DoChangeHistoryStatus(this,'del')" >Del</button>
                                                <button data-toggle="button" class="btn-sm btn-primary startBtn" type="button" onclick="DoChangeHistoryStatus(this,'start')" >Start</button>
                                            {{else}}
                                                <button data-toggle="button" class="btn-sm btn-danger delBtn" type="button" onclick="DoChangeHistoryStatus(this,'del')" >Del</button>
                                                <button data-toggle="button" class="btn-sm btn-primary startBtn" type="button" onclick="DoChangeHistoryStatus(this,'start')" >Start</button>
                                            {{end}}
                                        </p>

                                        <p>
                                            <a href="/db/detail?DbName={{$v.DbName}}&SchemaName={{$v.SchemaName}}&TableName={{$v.TableName}}">
                                                <button data-toggle="button" class="btn-sm btn-primary" type="button">Detail</button>
                                            </a>

                                            <a href="/flow/index?DbName={{$v.DbName}}&SchemaName={{$v.SchemaName}}&TableName={{$v.TableName}}">
                                                <button data-toggle="button" class="btn-sm btn-primary" type="button">Flow</button>
                                            </a>
                                        </p>
                                    </td>
                                </tr>
                            {{end}}
                            </tbody>
                        </table>
                    </div>


                    <div>
                        <p><strong>备注:</strong></p>
                        <p>1. 全量数据任务不会持久化,重启之后,任务会被清除掉</p>
                    </div>

                </div>

            </div>
        </div>

    </div>

</div>

<script type="text/javascript">
    var TableName="{{.TableName}}";
    var SchemaName = "{{.SchemaName}}";

    function DoRefresh() {
        var DbName = $("#DbName").val();
        var status = $("#status").val();
        window.location.href = "/history/index?DbName="+DbName+"&SchemaName="+SchemaName+"&TableName="+TableName+"&Status="+status;
    }

    $(function() {
        $("#DbName,#status").change(
            function () {
                DoRefresh();
            }
        );
    });

    function DoChangeHistoryStatus(obj,status){
        if (status=="stop"){
            if (!confirm("确定停止么？暂停后，可以继续点击开始任务进行！")){
                return
            }
        }
        if (status=="kill"){
            if (!confirm("确定强行kill当前任务？Kill任务后，当前表只能从0开始！")){
                return
            }
        }
        if (status=="del"){
            if (!confirm("确定 删除 么？删除将不能恢复")){
                return
            }
        }
        var url = "/history/"+status;
        var trObj = $(obj).parent().parent().parent();
        var DbName = trObj.children().eq(1).html();
        var Id = trObj.children().eq(0).html();
        var callback = function (data) {
            if(!data.status){
                alert(data.msg);
                return false;
            }
            if (status == "del"){
                trObj.remove();
            }else{
                location.reload();
            }
        };
        Ajax("POST",url, {DbName: DbName,Id:parseInt(Id)},callback,true);
    }
</script>


{{template "footer" .}}
